// import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { reqLogin, reqRegister, reqGetMessage } from './service';
import { history } from 'umi';
export default {
  namespace: 'login',
  state: {
    loading: undefined,
    vehicleMessage: [],
  },
  reducers: {
    setStatus(state, { payload }) {
      return { ...state, payload };
    },
    setVetMessage(state, { payload }) {
      return { ...state, vehicleMessage: payload };
    },
  },
  effects: {
    *login({ payload }, { call, put }) {
      const response = yield call(reqLogin, payload);
      if (response.code === 0) {
        if (!history) return;
        console.log('dadsa');
        const needMessage = yield call(reqGetMessage, payload);
        if (needMessage.code === 0) {
          yield put({
            type: 'setVetMessage',
            payload: needMessage.data,
          });
        }
        console.log('dasdasda');
        const { query } = history.location;
        const { redirect } = query;
        history.push(redirect || '/home');
        //切换路由
        localStorage.setItem('token', response.data.token);
        message.success('登陆成功');
      } else {
        message.error(response.msg);
      }
    },
    *register({ payload }, { call, put }) {
      const response = yield call(reqRegister, payload);
      if (response.code === 0) {
        if (!history) return;
        const { query } = history.location;
        const { redirect } = query;
        history.push(redirect || '/home');
        message.success('注册成功');
        //切换路由
      } else {
        message.error(response.msg);
      }
    },
  },
};
